home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10544 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Converting Strings to Upper Case
  5. Date: Mon, 18 Mar 96 16:14:03 GMT
  6. Organization: none
  7. Message-ID: <827165643snz@genesis.demon.co.uk>
  8. References: <4ifra6$52i@scipio.cyberstore.ca> <DoFA24.AL7@iquest.net> <4ii1nh$bng@castle.nando.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4ii1nh$bng@castle.nando.net>
  15.            actuary@nando.net "Bill McCarthy" writes:
  16.  
  17. >In <DoFA24.AL7@iquest.net>, dlmiller@iquest.net (Doug & Rose Miller) writes:
  18. >>ejw@news.cyberstore.ca () wrote:
  19. >>+Hi,
  20. >>+
  21. >>+
  22. >>+and the string would be modified.  I just can't seem to wrap my head around
  23. >>+the best way that I know is better than writing a for loop to check each
  24. >>+element in the array?
  25. >>+
  26. >>+I apologize if this in the FAQ;  I am still going thorugh it.  Any help
  27. >>+would be greatly appreciated.
  28. >>+
  29. >>+Eric Woodward.
  30. >>+ejw@cyberstore.ca.
  31. >>+
  32. >>
  33. >>
  34. >>#include <stdio.h>
  35. >>
  36. >>void main (void) {
  37. >
  38. >Oh, please!  Read the FAQ to avoid posting crap like this.
  39. >
  40. >>char    test[] = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
  41. >>char    *p;
  42. >>
  43. >>printf ("%s\n", &test);
  44. >
  45. >&test ???  How about just test?
  46. >
  47. >>for (p = test; *p; *p++)
  48. >
  49. >And what do you think you are accomplishing with *p++ that p++ or ++p
  50. >wouldn't do???
  51. >
  52. >>    if (isalpha(*p)) *p ^= 0x20;  /* for ascii machines e.g. PC; use 0x40 for
  53. > ebcdic machines e.g. IBM mainframe */
  54.  
  55. You could make the code itself more portable (and IMHO clearer) by writing
  56. this as:
  57.  
  58.       if (isalpha(*p)) *p ^= 'A' ^ 'a';
  59.  
  60. This still isn't guaranteed by the C language itself but it works for the
  61. character sets you are likely to encounter in the "C" locale. Whether such a
  62. hack is preferable over the 'correct' version below is questionable
  63. and depends on the use to which it is being applied.
  64.  
  65.       *p = isupper(*p) ? tolower(*p) : toupper(*p);
  66.  
  67. >isalpha() has not been declared.  It takes an int and *p may be a signed
  68. >char, how about *(unsigned char *)p.
  69.  
  70. It is certainly tempting to make p unsigned char *.
  71.  
  72. > Also, your code is toggling between
  73. >upper and lower case.
  74.  
  75. To be fair that looked like what was asked:
  76.  
  77. >>+I need to write a function to convert a string containg upper or lower case
  78. >>+characters to the opposite case.  Something like:
  79.  
  80. However the declarations suggest otherwise:
  81.  
  82. >>+  void libConvertUpperCase(char *str);        and
  83. >>+  void libConvertLowerCase(char *str);
  84.  
  85. -- 
  86. -----------------------------------------
  87. Lawrence Kirby | fred@genesis.demon.co.uk
  88. Wilts, England | 70734.126@compuserve.com
  89. -----------------------------------------
  90.